package team.szm.scd.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import team.szm.scd.database.dto.Result;
import team.szm.scd.database.dto.UserCreator;
import team.szm.scd.database.dto.UserLogin;
import team.szm.scd.database.entity.User;
import team.szm.scd.service.interfaces.ILoginService;
import team.szm.scd.service.interfaces.IUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    ILoginService loginService;

    //    @RequestMapping("/{id}")
//    public User fromId(@PathVariable String id) {
//        return userService.fromId(id);
//    }
    public static class FailMessages {
        public static final String NO_ID = "请输入身份证号或者医保号!";
        public static final String NO_VERIFY_CODE_IN_SESSION = "请刷新界面获取验证码！";
        public static final String WRONG_VERIFY_CODE = "验证码错误！";
        public static final String NO_USER_FIND = "请检查身份证号或者医保号是否正确，或者请先注册！";
        public static final String WRONG_PASSWORD = "密码错误!";
        public static final String EXCEPTION_IN_NEW_ACCOUNT = "此身份证号或医保号已被注册！";
    }

    @GetMapping("/now")
    @ResponseBody
    public Result<User> getNowUser(HttpSession session) {
        User user = loginService.getLoginUser(session);
        if (user == null) {
            return Result.needLogin();
        }
        return Result.success(user);
    }

    @GetMapping("/logout")
    @ResponseBody
    public void logout(HttpSession session) {
        session.setAttribute(User.USER_SESSION_STR, null);
    }

    @PostMapping("/new_account")
    @ResponseBody
    public Result<User> newAccount(@RequestBody UserCreator creator, HttpSession session) {
        Object verify_code = session.getAttribute("verify_code");
        if (!(verify_code instanceof String)) {
            return Result.fail(FailMessages.NO_VERIFY_CODE_IN_SESSION, null);
        } else if (!verify_code.equals(creator.getVerifyCode())) {
            return Result.fail(FailMessages.WRONG_VERIFY_CODE, null);
        }
        try {
            if (userService.addUser(creator)) {
                return Result.success(creator);
            }
        } catch (Exception e) {
            return Result.fail(FailMessages.EXCEPTION_IN_NEW_ACCOUNT, null);
        }
        return Result.fail((User) null);
    }

    @PostMapping("/login")
    @ResponseBody
    public Result<User> login(@RequestBody UserLogin login, HttpSession session) {
        User user;
        Object verify_code = session.getAttribute("verify_code");
        if (!(verify_code instanceof String)) {
            return Result.fail(FailMessages.NO_VERIFY_CODE_IN_SESSION, null);
        } else if (!verify_code.equals(login.getVerify_code())) {
            return Result.fail(FailMessages.WRONG_VERIFY_CODE, null);
        } else if (login.getId() != null) {
            user = userService.fromId(login.getId());
        } else if (login.getId2() != null) {
            user = userService.fromId2(login.getId2());
        } else {
            return Result.fail(FailMessages.NO_ID, null);
        }
        if (user == null) {
            return Result.fail(FailMessages.NO_USER_FIND, null);
        } else if (user.getPassword().equals(login.getPassword())) {
            session.setAttribute(User.USER_SESSION_STR, user);
            return Result.success(user);
        } else {
            return Result.fail(FailMessages.WRONG_PASSWORD, null);
        }
    }
}
